<template>
  <vxe-modal @close="cancel" :loading="loading" title="选择关联合同" v-model="dialogVisible" width="70%" showFooter esc-closable show-zoom
             resize>
    <div style="height: 75vh; overflow-y: auto;">
      <!-- 顶部搜索 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
        <el-form-item label="立项年度" prop="year">
          <el-select v-model="queryParams.year" placeholder="请选择立项年度" clearable>
            <el-option
              v-for="dict in dict.type.year"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="合同名称" prop="htName">
          <el-input
            v-model="queryParams.htName"
            placeholder="请输入合同名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
<!--        <el-form-item label="合同状态:" prop="processStatus">-->
<!--          <el-select v-model="queryParams.processStatus" placeholder="请选择合同状态" clearable size="mini">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.process_business_status"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="合同编号" prop="htBh">
          <el-input
            v-model="queryParams.htBh"
            placeholder="请输入合同编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table @row-click="clickRow" ref="table" highlight-current-row
                v-loading="loading" :data="htbzList" @selection-change="handleSelectionChange"
                :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}">
        <!--      <el-table-column type="selection" width="55" align="center"/>-->
        <el-table-column label="序号" align="center" type="index" width="50"/>
        <el-table-column label="年度" align="center" prop="year" width="80">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.year" :value="scope.row.year"/>
          </template>
        </el-table-column>
        <el-table-column label="合同编号" align="center" prop="htBh" show-overflow-tooltip/>
        <el-table-column label="合同名称" align="center" prop="htName" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleButtonClick(scope.row)"
            >
              <el-link type="primary">{{ scope.row.htName }}</el-link>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="课题归属" align="center" prop="ktGs" width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.kylx_ktgs" :value="scope.row.ktGs"/>
          </template>
        </el-table-column>
        <el-table-column label="承担单位" align="center" prop="cdDw" show-overflow-tooltip/>
        <el-table-column label="负责人" align="center" prop="fzName"/>
        <el-table-column label="项目总经费(万元)" align="center" prop="xmZjf" width="130"/>
        <el-table-column label="甲方支付(万元)" align="center" prop="jfZf"/>
        <!--      <el-table-column label="关联项目" align="center" prop="glXm"/>-->
        <!--      <el-table-column label="项目申报ID" align="center" prop="xmsbId"/>-->
        <el-table-column label="起时间" align="center" prop="qTime" width="130">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.qTime, '{y}-{m}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="止时间" align="center" prop="zTime" width="130">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.zTime, '{y}-{m}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="processStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus" />
          </template>
        </el-table-column>
        <el-table-column label="录入人员" align="center" prop="lrRy"/>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </div>

    <div slot="footer" class="dialog-footer dialog-footer-center">
      <el-button size="small" @click="submitForm">
        <svg-icon icon-class="confirm1" class-name='custom-class' />确 定
      </el-button>
      <el-button size="small" @click="cancel">
        <svg-icon icon-class="cancel1" class-name='custom-class' />取 消
      </el-button>
    </div>

  </vxe-modal>
</template>

<script>
import {listHtbz} from "@/api/xmgl/htgl/htbz/htbz";

export default {
  name: "htDx", //合同单选组件(已签订的合同)
  dicts: ['htbz_mj', 'rcpt_zc', 'kylx_zcry_ktfg', 'year', 'kylx_ktgs', 'process_business_status'],
  inject: ['updateHtLabel'],
  data() {
    return {
      loading: false,
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        htName: null,
        year: null,
        ktGs: null,
        glXm: null,
        xmsbId: null,
        processStatus: '3',
        htBh: null,
        cdDw: null,
        userId: this.$store.state.user.id,
        deptId: null
      },
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      selectedRow: null, // 保存当前选中的行
      htbzList: [],
      htLabel: null, //最后选择的合同
    }
  },
  methods: {
    handleOpen(){
      this.dialogVisible = true;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询合同编制列表 */
    getList() {
      this.loading = true;
      listHtbz(this.queryParams).then(response => {
        this.htbzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.dialogVisible = false
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.htbzId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
      this.htLabel = this.selectedRow
    },
    submitForm(){
      this.updateHtLabel(this.htLabel);
      this.dialogVisible = false;
    },
  }
}
</script>

<style scoped>

</style>
